﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;

namespace UCF.Core
{
    /// <summary>
    /// IIS帮助类
    /// </summary>
    public class IISHelper
    {
        /// <summary>
        /// 网站对象类
        /// </summary>
        public class WebSiteObject
        {
            /// <summary>
            /// 获取或设置端口
            /// </summary>
            public int Port { get; set; }
            /// <summary>
            /// 获取或设置默认文档
            /// </summary>
            public string DefaultDoc { get; set; }
            /// <summary>
            /// 获取或设置是否允许浏览目录
            /// </summary>
            public bool EnableDirBrowsing { get; set; }
            /// <summary>
            /// 获取或设置是否允许使用默认文档
            /// </summary>
            public bool EnableDefaultDoc { get; set; }
            /// <summary>
            /// 获取或设置是否允许运行脚本
            /// </summary>
            public bool AccessScript { get; set; }
            /// <summary>
            /// 获取或设置是否允许读取
            /// </summary>
            public bool AccessRead { get; set; }
            /// <summary>
            /// 获取或设置名称
            /// </summary>
            public string ServerComment { get; set; }
            /// <summary>
            /// 获取或设置路径
            /// </summary>
            public string Path { get; set; }
        }
        /// <summary>
        /// 获取网站列表
        /// </summary>
        /// <returns></returns>
        public IList<WebSiteObject> GetWebSiteList()
        {
            IList<WebSiteObject> list = new List<WebSiteObject>();
            /*
            DirectoryEntry service = new DirectoryEntry("IIS://" + Environment.MachineName + "/W3SVC");
            DirectoryEntry iis;
            DirectoryEntry iisRoot;
            IEnumerator ieWebsites = service.Children.GetEnumerator();
            IEnumerator ieRoot;
            WebSiteObject ws;
            while (ieWebsites.MoveNext())
            {
                iis = (DirectoryEntry)ieWebsites.Current;
                if (iis.SchemaClassName == "IIsWebServer")
                {
                    ws = new WebSiteObject();
                    ws.DefaultDoc = iis.Properties["ServerComment"][0].ToString();
                    ws.AccessRead = (bool)iis.Properties["AccessRead"][0];
                    ws.AccessScript = (bool)iis.Properties["AccessScript"][0];
                    ws.DefaultDoc = (string)iis.Properties["DefaultDoc"][0];
                    ws.EnableDefaultDoc = (bool)iis.Properties["EnableDefaultDoc"][0];
                    ws.EnableDirBrowsing = (bool)iis.Properties["EnableDirBrowsing"][0];
                    ws.Port = Convert.ToInt32(((string)iis.Properties["Serverbindings"][0]).Substring(1, ((string)iis.Properties["Serverbindings"][0]).Length - 2));
                    ieRoot = iis.Children.GetEnumerator();
                    while (ieRoot.MoveNext())
                    {
                        iisRoot = (DirectoryEntry)ieRoot.Current;
                        if (iisRoot.SchemaClassName == "IIsWebVirtualDir")
                        {
                            ws.Path = iisRoot.Properties["path"][0].ToString();
                        }

                    }
                    list.Add(ws);
                }
            }
             */
            return list;
        }
        /// <summary>
        /// 创建网站
        /// </summary>
        /// <param name="site"></param>
        public void AddWebsite(WebSiteObject site)
        {
            /*
            if (site.ServerComment == null) throw new Exception("网站名称不能为空");
            DirectoryEntry service = new DirectoryEntry("IIS://" + Environment.MachineName + "/W3SVC");
            DirectoryEntry iis;
            IEnumerator ieWebsites = service.Children.GetEnumerator();
            while (ieWebsites.MoveNext())
            {
                iis = (DirectoryEntry)ieWebsites.Current;
                if (iis.SchemaClassName == "IIsWebServer")
                {
                    iis = service.Children.Add("0", "IIsWebServer");
                    iis.Properties["ServerComment"][0] = site.ServerComment;
                    iis.Properties["Serverbindings"].Add(":" + site.Port + ":");
                    iis.Properties["AccessScript"][0] = site.AccessScript;
                    iis.Properties["AccessRead"][0] = site.AccessRead;
                    iis.Properties["EnableDirBrowsing"][0] = site.EnableDirBrowsing;
                    iis.Properties["DefaultDoc"][0] = site.DefaultDoc;
                    iis.Properties["EnableDefaultDoc"][0] = site.EnableDefaultDoc;

                    DirectoryEntry root = iis.Children.Add("Root", "IIsWebVirtualDir");
                    root.Properties["path"][0] = site.Path;

                    iis.CommitChanges();
                    iis.CommitChanges();
                    root.CommitChanges();
                }
            }
             */
        }
    }
}
